package net.mysoft.budgetsoft.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.FlowTemplate;
import net.mysoft.budgetsoft.system.service.FlowTemplateService;
import net.mysoft.framework.authority.AccessType;
import net.mysoft.framework.authority.Permission;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.model.TreeNode;
import net.mysoft.framework.util.ExceptionUtil;
import net.mysoft.framework.util.TreeUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-08 17:26:47 
 */
@Action(value = "flowTemplateAction", results = { @Result(name = "flowTemplate", location = "/platform/system/flowTemplate.jsp"), @Result(name = "flowTemplateAdd", location = "/platform/system/flowTemplateAdd.jsp"), @Result(name = "flowTemplateEdit", location = "/platform/system/flowTemplateEdit.jsp"), @Result(name = "flowTemplateView", location = "/platform/system/flowTemplateView.jsp")})
@Controller("flowTemplateAction")
public class FlowTemplateAction extends BaseAction implements ModelDriven<FlowTemplate>{

	private static final Logger logger = Logger.getLogger(FlowTemplateAction.class);
	@Resource
	private FlowTemplateService flowTemplateService;
	
	private FlowTemplate flowTemplate = new FlowTemplate();
	
	public FlowTemplateService getFlowTemplateService(){
		return this.flowTemplateService;
	}
	
	public void setFlowTemplateService(FlowTemplateService flowTemplateService){
		this.flowTemplateService = flowTemplateService;
	}
	
	public FlowTemplate getFlowTemplate(){
		return this.flowTemplate;
	}
	
	public void setFlowTemplate(FlowTemplate flowTemplate){
		this.flowTemplate = flowTemplate;
	}
	
	public FlowTemplate getModel() {
		return flowTemplate;
	}
	
	/**
	 * FlowTemplate列表页面
	 * @return
	 */
	public String flowTemplate() {
		return "flowTemplate";
	}
	
	/**
	 * FlowTemplate查看页面
	 * @return
	 */
	public String flowTemplateView() {
		return "flowTemplateView";
	}
	
	/**
	 * FlowTemplate添加页面
	 * @return
	 */
	public String flowTemplateAdd() {
		return "flowTemplateAdd";
	}
	
	/**
	 * FlowTemplate编辑页面
	 * @return
	 */
	public String flowTemplateEdit() {
		return "flowTemplateEdit";
	}
	
	/**
	 * 添加
	 */
	public void add() {
		Json j = new Json();
		try {
			flowTemplateService.add(flowTemplate);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 修改
	 */
	public void edit() {
		Json j = new Json();
		try {
			flowTemplateService.edit(flowTemplate);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 删除
	 */
	public void delete() {
		Json j = new Json();
		try {
			flowTemplateService.delete(flowTemplate);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 列表
	 */
	public void datagrid() {
		try {
			super.writeJson(flowTemplateService.datagrid(flowTemplate));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
	/**
	 * 取消发布
	 */
	public void unpublish(){ 
		logger.info("取消发布");
		Json j = new Json();
		try {
			flowTemplateService.unpublish(flowTemplate);
			j.setSuccess(true);
			j.setMsg("取消发布成功！"); 
		} catch (Exception e) {
			j.setMsg("取消发布失败！");
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}
	/**
	 * 发布
	 */
	public void publish(){
		Json j = new Json();
		try {
			flowTemplateService.publish(flowTemplate);
			j.setSuccess(true);
			j.setMsg("发布成功！"); 
		} catch (Exception e) {
			j.setMsg("发布失败！");
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}
	/**
	 * 树
	 */
	@Permission(access=AccessType.LOGIN)
	public void tree(){
		List<TreeNode> tree = new ArrayList<TreeNode>();
		try {
			List<FlowTemplate> list = flowTemplateService.find(flowTemplate);
			TreeNode rootNode = new TreeNode();
			rootNode.setId("0");  
			rootNode.setParentId("root");
			rootNode.setText("系统流程模板");
			tree.add(rootNode);
			for(FlowTemplate flowTemplate : list){
				TreeNode node = new TreeNode();
				node.setId(flowTemplate.getFtl_ID());  
				node.setParentId("0");
				node.setText(flowTemplate.getFtl_Name()); 
				tree.add(node);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.writeJson(TreeUtil.generateTree(tree,new ArrayList(),"root"));
	}
	
	@Permission(access=AccessType.LOGIN)
	public void find(){
		List<FlowTemplate> list = flowTemplateService.find(flowTemplate);
		super.writeJson(list);
	}
	
}
